//---------------------------------------------------------------------------------
// Copyright (c) 2011, Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------------

namespace Microsoft.Samples.ServiceBus.WindowsPhoneChat
{
    using System;
    using System.Windows;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using Microsoft.Samples.ServiceBus.WindowsPhoneChat.ViewModels;

    public partial class MainPage : PhoneApplicationPage
    {
        private readonly MainPageViewModel viewModel;

        public MainPage()
        {
            InitializeComponent();
            this.ApplicationBar = this.CreateAppBar();

            this.viewModel = new MainPageViewModel();
            this.viewModel.AppBarVisibilityChanged += this.OnAppBarVisibilityChanged;
            this.viewModel.DialogShown += this.OnDialogShown;

            this.DataContext = this.viewModel;
        }

        public MainPageViewModel ViewModel
        {
            get { return this.viewModel; }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // Show 'Navigate in' animation
            VisualStateManager.GoToState(this, "Visible", true);
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            // Show 'Navigate out' animation
            VisualStateManager.GoToState(this, "NotVisible", true);

            base.OnNavigatingFrom(e);
        }

        private ApplicationBar CreateAppBar()
        {
            var newButton = new ApplicationBarIconButton(new Uri("/Resources/AppBar/new.png", UriKind.Relative)) { Text = "New Topic" };
            newButton.Click += this.OnNewButtonClicked;

            var refreshButton = new ApplicationBarIconButton(new Uri("/Resources/AppBar/refresh.png", UriKind.Relative)) { Text = "Refresh" };
            refreshButton.Click += this.OnRefreshButtonClicked;

            return new ApplicationBar()
            {
                IsVisible = false,
                Buttons = 
                {
                    newButton,
                    refreshButton
                }
            };
        }

        private void OnAppBarVisibilityChanged(object sender, EventArgs e)
        {
            this.Dispatcher.BeginInvoke(() => this.ApplicationBar.IsVisible = this.viewModel.ShowAppBar);
        }

        private void OnDialogShown(object sender, ShowDialogEventArgs e)
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                this.addTopicDialog.DataContext = e.DialogViewModel;
                this.addTopicDialog.Callback = e.Callback;
                this.addTopicDialog.State = e.State;

                VisualStateManager.GoToState(this, e.DialogName, true);
            });
        }

        private void OnNewButtonClicked(object sender, EventArgs e)
        {
            this.viewModel.BeginCreate();
        }

        private void OnRefreshButtonClicked(object sender, EventArgs e)
        {
            this.viewModel.BeginRefresh();
        }
    }
}
